<?php
/**
 * @权限类
 * 
 * 一、权限
 * 1、权限列表
 * 
 * 二、角色
 * 1、角色列表
 * 2、角色详情
 * 3、角色编辑/添加
 * 4、角色删除
 * 5、角色开启禁用

 * 
 * 三、
 * 1、管理员列表
 * 2、管理员详情
 * 3、管理员编辑/添加
 * 4、管理员删除
 * 5、管理员开启禁用
 * 
 * @作者 
 * 万全强（php）
 */
namespace app\controller;

use app\controller\HomeBase;
use think\facade\Db;

use app\validate\RoleValidate;
use app\model\Role;
use app\model\RoleMenu;
use app\model\Chain;


use app\validate\AdminValidate;
use app\model\Admin;
use app\model\AdminRole;

use app\model\CityModel;

class Permiss extends HomeBase
{

    private $RoleValidate;
    private $roleModel;
    private $RoleMenuModel;
    private $chainModel;


    private $AdminValidate;
    private $adminModel;
    private $AdminRoleModel;
    

    public function __construct(){
               
        parent::__construct();
         
         $this->RoleValidate=new RoleValidate();
         $this->roleModel=new Role();
         $this->RoleMenuModel=new RoleMenu();
         $this->chainModel=new Chain();

         $this->AdminValidate=new AdminValidate();
         $this->adminModel=new Admin();
         $this->AdminRoleModel=new AdminRole();

    }
    /**
     * 权限列表
     *
     * @return void
     */
    public function GetMenu()
    {
        $param=request()->param();
        $where="1=1 ";
        if(isset($param['keywords'])&&empty($param['keywords'])){
            $where.=" and a.menu_name like '%".trim($param['keywords'])."%'";
        }
        
        
        $res=$this->roleModel->GetMenu($where,$this->user_id);
        return json(['status'=>1,'msg'=>'获取成功','data'=>$res]);
    }
    /**
     * 角色列表
     *
     * @return void
     */
    public function GetRoleList()
    {
        $param=request()->param();
        $where="1=1 and status !=-1 ";
        if(isset($param['keywords'])&&!empty($param['keywords'])){
            $where.=" and role_name like '%".trim($param['keywords'])."%'";
        }
        if(isset($param['status'])&&strlen($param['status'])>0){
            //p(1);
            $where.=" and status = '{$param['status']}'";
        }

        $pageSize=(isset($param['pagesize'])&&!empty($param['pagesize']))?$param['pagesize']:15;
        
        $res=Db::name('role')->field('id,role_name,status,create_time,is_default')->where($where)->paginate($pageSize);

        $return['total']=$res->total();
        $return['current_page']=$res->currentPage();
        $return['last_page']=$res->lastPage();
        $return['per_page']=$res->listRows();
        $return['data']=$res->items();

        foreach ($return['data'] as $key => &$value) {
            $value['AdminNum']=Db::name('admin')->alias('a')->join('admin_role b','a.id=b.admin_id','left')->where(['b.role_id'=>$value['id'],'a.status'=>1,'b.status'=>1])->count('a.id');
            $value['url']=config('app.AppUrl')."?id=".$value['id'];
        }

        return json(['status'=>1,'msg'=>'获取成功','data'=>$return]);
    }
    /**
     * 角色详情
     *
     * @return void
     */
    public function GetRoleDetail()
    {
        $param=request()->param();
        $id=isset($param['id'])?intval($param['id']):0; 
        $result=$this->roleModel->detail($id);
        if(!empty($result)){
           $result['Menu']=$this->RoleMenuModel->GetMenu($id);//当前权限
           $chain=$this->chainModel->GetChain($id);//当前产业链
           $result['Chain']=arr2tree($chain,0,'chain_name');
           
           $CityModel=new CityModel();
           $city=$CityModel->GetDetail($result['city_id']);
           $result['CityName']=empty($city)?'全球':$city['city_name'];
        }
        return json(['status'=>1,'msg'=>'获取成功','data'=>$result]);

    }
    /**
     * 角色添加/编辑
     *
     * @return void
     */
    public function RoleEdit()
    {
        $param=request()->param();
        $id=isset($param['id'])?intval($param['id']):0;
    
        //表单验证
        try {
           $re=$this->validate($param,'app\validate\RoleValidate');
        } catch (\Exception $e) {       
            return json(['status'=>0,'msg'=>$e->error,'data'=>[]]);
        }
        
        //角色表
        if($id){
            $id= $this->roleModel->EditData($param,$id);
        }else{
            $id= $this->roleModel->AddData($param);
        }
       
        //角色对应菜单
        if(isset($param['menu_ids'])&&!empty($param['menu_ids'])){
            $menu=explode(',',$param['menu_ids']);
            $this->RoleMenuModel->DealRel($id,$menu);
        }

        //角色对应产业链
        if(isset($param['chain_ids'])&&!empty($param['chain_ids'])){
            $chain_ids=json_decode($param['chain_ids'],true); 
            //形成产业链关系        
            $this->chainModel->DealRel($id,$chain_ids);

            //修改
            $this->chainModel->UpdatePoint($id);
        }


        return json(['status'=>1,'msg'=>'操作成功','data'=>['id'=>$id]]);
    }
    /**
     * 角色删除
     *
     * @return void
     */
    public function RoleDel()
    {
        
        $param=request()->param();
        $id=isset($param['id'])?trim($param['id']):'';
        
        
        $result=$this->roleModel->Del(explode(',',$id));
        if($result){
            return json(['status'=>1,'msg'=>'删除成功','data'=>[]]);
        }else{
            return json(['status'=>0,'msg'=>'删除失败','data'=>[]]);
        }
    }
    /**
     * 角色是否开启禁用
     *
     * @return void
     */
    public function UpdateRoleStatus()
    {
        $param=request()->param();
        $id=isset($param['id'])?intval($param['id']):0;
        
        $result=$this->roleModel->UpdateStatus($id);
        if($result){
            return json(['status'=>1,'msg'=>'操作成功','data'=>[]]);
        }else{
            return json(['status'=>0,'msg'=>'操作失败','data'=>[]]);
        }
    }
    /**
     * 角色设置默认
     *
     * @return void
     */
    public function SetDefault()
    {
        $param=request()->param();
        $id=isset($param['id'])?intval($param['id']):0;
        
        $result=$this->roleModel->SetDefault($id);
        if($result['status']==1){
            return json(['status'=>1,'msg'=>$result['msg'],'data'=>[]]);
        }else{
            return json(['status'=>0,'msg'=>$result['msg'],'data'=>[]]);
        }
    }
    /**
     * 管理员列表
     *
     * @return void
     */
    public function GetAdminList()
    {
        $param=request()->param();
        $where="1=1 and status !=-1";
        if(isset($param['keywords'])&&!empty($param['keywords'])){
            $where.=" and nickname like '%".trim($param['keywords'])."%'";
        }
        if(isset($param['status'])&&strlen($param['status'])>0){
            $where.=" and status = '{$param['status']}'";
        }
        
        $res=$this->adminModel->GetList($where);
        return json(['status'=>1,'msg'=>'获取成功','data'=>$res]);
    }
    /**
     * 管理员详情
     *
     * @return void
     */
    public function GetAdminDetail()
    {
        $param=request()->param();
        $id=isset($param['id'])?intval($param['id']):0; 
        $result=$this->adminModel->detail($id);
        if(!empty($result)){
           $result['role_ids']=$this->AdminRoleModel->GetRole($id);
        }
        return json(['status'=>1,'msg'=>'获取成功','data'=>$result]);
       
    }
    /**
     * 管理员添加/编辑
     *
     * @return void
     */
    public function AdminEdit()
    {
        $param=request()->param();
        $id=isset($param['id'])?intval($param['id']):0;
    
        //表单验证
        try {
           $re=$this->validate($param,'app\validate\AdminValidate');
        } catch (\Exception $e) {       
            return json(['status'=>0,'msg'=>$e->error,'data'=>[]]);
        }
        
        //管理表
        if($id){
            $id= $this->adminModel->EditData($param,$id);
        }else{
            $id= $this->adminModel->AddData($param);
        }
       
        //管理对应角色
        if(isset($param['role_ids'])&&!empty($param['role_ids'])){
            $menu=explode(',',$param['role_ids']);
            $this->AdminRoleModel->DealRel($id,$menu);
        }


        return json(['status'=>1,'msg'=>'操作成功','data'=>['id'=>$id]]);
    }
    /**
     * 管理员删除
     *
     * @return void
     */
    public function AdminDel()
    {
        $param=request()->param();
        $id=isset($param['id'])?trim($param['id']):'';
        
        
        $result=$this->adminModel->Del(explode(',',$id));
        if($result){
            return json(['status'=>1,'msg'=>'删除成功','data'=>[]]);
        }else{
            return json(['status'=>0,'msg'=>'删除失败','data'=>[]]);
        }
    }
    /**
     * 管理员是否开启禁用
     *
     * @return void
     */
    public function UpdateAdminStatus()
    {
        $param=request()->param();
        $id=isset($param['id'])?intval($param['id']):0;
        
        $result=$this->adminModel->UpdateStatus($id);
        if($result){
            return json(['status'=>1,'msg'=>'操作成功','data'=>[]]);
        }else{
            return json(['status'=>0,'msg'=>'操作失败','data'=>[]]);
        }
    }
    /**
     * test
     */
    public function test1(){
        $role_id=50;
        $data=$this->chainModel->UpdatePoint($role_id);
        p($data);
    }
}
